import json
import os

import requests
from django.test import TestCase

from utils.vendor.ip2Region import Ip2Region
from utils.vendor.s3Tool import S3Tool


#
class UtilsTest(TestCase):
    # python manage.py test utils.tests.UtilsTest.qj_test_count
    def qj_test_count(args):
        for i in range(20,1000):
            print("page:"+str(i))
            res=requests.get('http://smartgym.com/api/test/temp12',{'page':i})
            print(res.text)

    # python manage.py test utils.tests.UtilsTest.test_ip2region
    # 测试根据ip 获取 对应的地址
    def test_ip2region(self):
        ip="39.106.66.201"
        path=os.path.dirname(__file__)+"\data\ip2region.db"
        instance=Ip2Region(path)
        addr=instance.memorySearch(ip) # 内存查找
        addr1=instance.binarerySearch(ip) # 二分法查找
        addr2=instance.btreeSearch(ip) # 树形结构查找
        print(addr['region'].decode(),addr1['region'].decode(),addr2['region'].decode())

    """
    场馆智能删除2019年的入场图片 python manage.py test utils.tests.UtilsTest.del_2019
    """
    def del_2019(self):
        for x in range(1294,3000):
            self.delete_2019_file(page=x)


    def delete_2019_file(self,page):
        res=requests.get('http://smartgym.com/api/test/delete_2019_aws_check_in',{'page':page})
        req_file=os.getcwd()+"/utils/data/del/"+str(page)+'.json' # 请求文件
        rsp_file=os.getcwd()+"/utils/data/del/"+str(page)+'_log.json' # 请求文件
        with open(req_file,'a+') as file:
            file.write(res.text)
        
        res=json.loads(res.text)
        ocmgym=[]
        ocmdoor=[]
        ocmface=[]
        if(res.get('flag')):
            client=S3Tool()
            data=res.get('data')
            if len(data)<1:
                return
            temp=data.get('ocmgym',[])
            for x in temp:
                ocmgym.append({'Key':x})
            if len(ocmgym)>0:
                temp=self.chunk(lst=ocmgym,size=999)
                with open(rsp_file,'a+') as f:
                    for r in temp:
                        print("ocmgym")
                        f.write(json.dumps(client.del_file(bucket_name='ocmgym',delete_files=r))) 

            res=data.get('ocmdoor',[])
            for x in res:
                ocmdoor.append({'Key':x})  
            if len(ocmdoor)>0:
                temp=self.chunk(lst=ocmdoor,size=999)
                with open(rsp_file,'a+') as f:
                    for r in temp:
                        print("ocmdoor")
                        f.write(json.dumps(client.del_file(bucket_name='ocmdoor',delete_files=r)))    

            res=data.get('ocmface',[])
            for x in res:
                ocmface.append({'Key':x})
            if len(ocmface)>0:
                temp=self.chunk(lst=ocmface,size=999)
                with open(rsp_file,'a+') as f:
                    for r in temp:
                        print("ocmface")
                        f.write(json.dumps(client.del_file(bucket_name='ocmface',delete_files=r))) 

        print(len(ocmgym),len(ocmdoor),len(ocmface))


    """
    删除aws 图片 python manage.py test utils.tests.UtilsTest.test_s3_del_file
    可删除的数据路径
            ocmlog/*
    """
    def test_s3_del_file(self):
        client=S3Tool()
        p1="ocmgym"
        
        
        p2=[{'Key':'511/2019/3/1/54425_2500759.jpg'}]
        print(client.del_file(bucket_name=p1,delete_files=p2))

    """
    删除aws 路径 python manage.py test utils.tests.UtilsTest.test_s3_del_dir

        人脸入场图片路径 ocmface  和 ocmdoor
        人脸模板图片路径  ocmcomface 目录下
        私教图片路径 ocmgym 和 ocmface
        手环机图片路径 ocmgym
    """
    def test_s3_del_dir(self):
        client=S3Tool()
        p1="ocmlog"
        p2=["XLAccess/","TouchRobot/"]
        print(client.del_dir(bucket_name=p1,dir_paths=p2))

    """
    python partition list
    :param lst: list
    :param n: partitionSize
    :return:
    """
    def partition(self,lst, n):
        division = len(lst) / float(n)
        return [list(lst)[int(round(division * i)): int(round(division * (i + 1)))] for i in range(n)]

    """
    python chunk  list
    :param lst: list
    :param size
    :return:
    """
    def chunk(self,lst, size):
        return [list(lst)[int(round(size * i)): int(round(size * (i + 1)))] for i in range(int(len(lst) / float(size)) + 1)]
